package com.cnpinxi.service;

import com.cnpinxi.dao.ActivityMapper;
import com.cnpinxi.domain.Activity;
import com.cnpinxi.myRunTime.MyRunTimeExcption;
import com.cnpinxi.service.*;
import com.cnpinxi.tool.Tools;
import com.cnpinxi.util.DateUtil;
import com.cnpinxi.util.GetProperties;
import com.cnpinxi.util.QRCodeUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created by Baggio on 2017-11-28.
 */
@Service
public class ActivityServiceImpl implements com.cnpinxi.service.ActivityService {
    @Autowired
    ActivityMapper activityMapper;


    @Override
    public void modifyActivity(Map<String, String> map) {
        String id = map.get("tid");
        Activity activity = null;

        if (StringUtils.isBlank(id)) {
            activity = new Activity();
            activity.setCreateTime(new Date());
        } else {
            activity = activityMapper.getActivityById(Long.parseLong(id));
        }

       /* String productId = map.get("productId");
        if (StringUtils.isNotBlank(productId)) {
            activity.setProductId(Long.parseLong(productId));
        }*/
        String activityType = map.get("activityType");
        if (StringUtils.isNotBlank(activityType)) {
            activity.setActivityType(Byte.parseByte(activityType));
        }
        String discountsType = map.get("discountsType");
        if (StringUtils.isNotBlank(discountsType)) {
            activity.setDiscountsType(Byte.parseByte(discountsType));
        }
        String activityName = map.get("activityName");
        if (StringUtils.isNotBlank(activityName)) {
            activity.setActivityName(activityName);
        }
        String activityUrl = map.get("activityUrl");
        if (StringUtils.isNotBlank(activityUrl)) {
            activity.setActivityUrl(activityUrl);
        }
        String sourceQrCode = map.get("sourceQrCode");
        if (StringUtils.isNotBlank(sourceQrCode)) {
            activity.setSourceQrCode(sourceQrCode);
        }
        String startDate = map.get("startDate");
        if (StringUtils.isNotBlank(startDate)) {
            activity.setStartDate(DateUtil.strToDate(startDate, DateUtil.DATE));
        }
        String endDate = map.get("endDate");
        if (StringUtils.isNotBlank(endDate)) {
            activity.setEndDate(DateUtil.strToDate(endDate, DateUtil.DATE));
        }
        String price = map.get("price");
        System.out.println("price======================================>>>>>>"+price);
        if (StringUtils.isNotBlank(price)) {
            activity.setPrice(Integer.parseInt(price));
        }
        if (StringUtils.isBlank(id)) {
             insertActivity(activity);
        } else {
            activityMapper.updateActivityById(activity);
        }
       /* if(activity.getTid()==null){
            insertActivity(activity);
            return;
        }
        updateActivity( activity);*/
    }

    private void insertActivity(Activity activity){activityMapper.insertActivity(activity);}
    private void updateActivity(Activity activity){activityMapper.updateActivityByPrimaryKeySelective(activity);}
    @Override
    public List<Activity> getActivityList(Map<String, String> map) {
        return activityMapper.getActivityList(map);
    }

    @Override
    public Activity getActivity(Map<String, String> map) {
        return activityMapper.getActivity(map);
    }

    @Override
    public Activity getActivityById(Long tid) {
        return activityMapper.getActivityById(tid);
    }

    @Override
    public int insertActivityProductEntityRelation(Map<String, String> map) {
        if( activityMapper.isExist(map)>0){
            throw new MyRunTimeExcption("活动已关联该产品，请勿重复操作！");
        }
        return activityMapper.insertActivityProductEntityRelation(map);
    }

    @Override
    public int isExist(Map<String, String> map) {
        return activityMapper.isExist(map);
    }

    @Override
    public int createActivityQRCode(Map<String, String> map,String basePath) {
        String activityUrl = map.get("activityUrl");
        String activityId = map.get("activityId");
        String text =  basePath+"/" +activityUrl+"?activityId="+activityId; // 二维码内容
        String activityName = Tools.getUUID();
        //logo文件地址
        String logoPath = "C:/Users/lutao/Pictures/0.jpg";
        String  picPath= GetProperties.readValue("picpath");
        String filePath=picPath+activityName+".jpg";
        System.out.println("filePath====>>>>"+filePath);
        try {
           //生成带LOGO 的二维码图片
           QRCodeUtil.getLogoQRCode(text,logoPath,"jpg",filePath);
        } catch (Exception e) {
            e.printStackTrace();
        }
        Activity activity = new Activity();
        activity.setSourceQrCode(activityName+".jpg");//二维码图片
        activity.setTid(Integer.parseInt(activityId));
        return activityMapper.updateActivityByPrimaryKeySelective(activity);
    }

}
